<?php

namespace app\publics\controller\api;

use app\ApiController;
use app\mainadmin\model\MessageModel;
use app\mainadmin\model\MessageUserModel;
use think\Db;

/*------------------------------------------------------ */
//-- 站内消息相关API
/*------------------------------------------------------ */

class Message extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
    }

    /*------------------------------------------------------ */
    //-- 获取消息列表
    /*------------------------------------------------------ */
    public function getList()
    {
        $state = input('state', 'waitCheck', 'trim');
        $MessageModel = new MessageModel();
        $this->sqlOrder = 'send_time DESC';
        if ($state == 'my') {
            $MessageUserModel = new MessageUserModel();
            $where = [];
            $where[] = ['user_id', '=', $this->userInfo['user_id']];
            $where[] = ['send_time', '<', time()];
            $where[] = ['show_end_time', '>', time()];
            $data = $this->getPageList($MessageUserModel, $where, '', 8);
            foreach ($data['list'] as $key => $row) {
                if ($key == 0) {
                    if ($row['is_read'] == 0) {
                        Db::name('main_message_user')->where(array('rec_id' => $row['rec_id']))->update(array('is_read' => 1));
                    }
                }
                $row['send_time'] = dateTpl($row['send_time']);
                if ($row['type'] == 0) {
                    $message = $MessageModel->where('message_id', $row['ext_id'])->find();
                    if ($message['article_id'] > 0) {
                        $row['ext_id'] = $message['article_id'];
                    } else {
                        $row['ext_id'] = 0;
                    }
                }
                $data['list'][$key] = $row;
            }
            return $this->success($data);
        } else {//系统公告

            $where = [];
            $where[] = ['type', '=', 'all'];
            $where[] = ['send_time', '<', time()];
            $where[] = ['show_end_time', '>', time()];
            $data = $this->getPageList($MessageModel, $where, '', 8);
            $log_where['user_id']=$this->userInfo['user_id'];
            foreach ($data['list'] as $key => $row) {
                $log_where['message_id']=$row['message_id'];
                $main_message_log=Db::name('main_message_log')->where($log_where)->count();
                if ($key == 0) {
                    if ($main_message_log == 0) {
                        Db::name('main_message_log')->insert($log_where);
                    }
                }
                $row['send_time'] = dateTpl($row['send_time']);
                $data['list'][$key] = $row;
            }
        }
        return $this->success($data);
    }

    /*------------------------------------------------------ */
    //-- 判断消息是否已读
    /*------------------------------------------------------ */
    public function isRead()
    {
        ####判断最新一条数据是否已读
        ###########################################我的消息#################################################
        $where[] = ['user_id', '=', $this->userInfo['user_id']];
        $main_message_user = Db::name('main_message_user')->where($where)->field('is_read')->order('rec_id desc')->find();

        ###########################################系统公告#################################################
        $where_m = [];
        $where_m[] = ['type', '=', 'all'];
        $where_m[] = ['send_time', '<', time()];
        $where_m[] = ['show_end_time', '>', time()];
       ##查询最新一条系统消息记录
        $main_message_id=Db::name('main_message')->where($where_m)->field('message_id')->order('message_id desc')->find();
       ##查询用户是否查看了系统消息
        if ($main_message_id) {
            $log_where['user_id'] = $this->userInfo['user_id'];
            $log_where['message_id'] = $main_message_id['message_id'];
            $main_message_log = Db::name('main_message_log')->where($log_where)->count();
            if ($main_message_log == 0) {
                $main_message_user['is_read'] = 0;
            }
        }

        return $this->success($main_message_user);
    }


}

